package org.link.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.link.bean.User;
import org.link.util.DBUtil;
import org.link.util.DateUtil;

/**
 * 
 * project_name:forumweb
 * package_name:org.link.dao
 * file_name:UserDao.java
 * @author Link
 * create_date:2015年10月22日 上午10:40:47
 * modified_date:
 * description: 用户实体类dao方法
 *
 */
public class UserDao {

	/**
	 * 
	 * description: 判断输入的密码是否正确
	 * 2015年10月22日 下午3:08:22
	 * @author Link
	 * @param email
	 * @param pass
	 * @return
	 *
	 */
	@SuppressWarnings("resource")
	public boolean checkPassCorrect(String email,String pass){
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select pass from t_user where email=?");
			pstmt.setString(1, email);
			rs = pstmt.executeQuery();
			while(rs.next()){
				if(rs.getString("pass").equals(pass)){
					return true;//true代表输入的密码正确
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return false;//false代表输入的密码不正确
		
	}
	
	/**
	 * 
	 * description:根据传入的邮箱名找到对应的id号
	 * 2015年10月27日 下午5:53:42
	 * @author Link
	 * @param email
	 * @return
	 *
	 */
	public int queryUserId(String email){
		int result = 0;
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select id from t_user where email=?");
			pstmt.setString(1, email);
			rs = pstmt.executeQuery();
			while(rs.next()){
				result = rs.getInt("id");
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return result;
	}
	
	
	
	/**
	 * 
	 * description: 判断输入邮箱是否已存在
	 * 2015年10月22日 上午10:42:56
	 * @author Link
	 * @param email
	 * @return
	 *
	 */
	@SuppressWarnings("resource")
	public boolean checkEmailExist(String email){
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select email from t_user where email=? and isdelete=0 and isactive=1");
			pstmt.setString(1, email);
			rs = pstmt.executeQuery();
			while(rs.next()){
				if(rs.getString("email").equals(email)){
					return true;//true代表输入的email存在
				}
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return false;//false代表输入的email不存在
	}
	
	/**
	 * 
	 * description: 根据输入的邮箱查询并返回用户信息
	 * 2015年10月22日 下午3:22:18
	 * @author Link
	 * @param email
	 * @return
	 *
	 */
	public User selectUserInfo(String email){
		User user = new User();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select id,ip from t_user where email=?");
			pstmt.setString(1, email);
			rs = pstmt.executeQuery();
			while(rs.next()){
				user.setId(rs.getInt("id"));
				user.setIp(rs.getString("ip"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn, rs);
		}
		return user;
	}
	
	/**
	 * 
	 * description: 根据登录用户的email更新数据库中的ip地址
	 * 2015年10月22日 下午3:29:14
	 * @author Link
	 * @param email
	 * @return
	 *
	 */
	public void updateIp(String email,String ip){
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement("update t_user set ip=?,isonline=1 where email=?");
			pstmt.setString(1, ip);
			pstmt.setString(2, email);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn);
		}	
	}
	
	/**
	 * 
	 * description: 添加管理员帐号
	 * 2015年10月22日 上午11:53:45
	 * @author Link
	 * @param user
	 * @return
	 *
	 */
	public int addManager(User user){
		int result = 1;
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement("insert into t_user(email,pass,regtime)values(?,?,?)");
			pstmt.setString(1, user.getEmail());
			pstmt.setString(2, user.getPass());
			pstmt.setString(3, user.getRegtime());
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn);
			
		}
		return result;
	}
	
	/**
	 * 
	 * description: 添加用户
	 * creatTime:2015年10月24日下午9:18:00
	 * @author Link
	 * return type: int
	 *
	 */
	public int addUser(String email,String pass){
		int result = -1;
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement("insert into t_user(email,pass,regtime)values(?,?,?)");
			pstmt.setString(1, email);
			pstmt.setString(2, pass);
			pstmt.setString(3, DateUtil.getDate(new Date()));
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn);
		}
		return result;
	}
	/**
	 * 
	 * description: 激活用户dao方法
	 * creatTime:2015年10月24日下午10:22:50
	 * @author Link
	 * return type: int
	 *
	 */
	public int activeUser(String email){
		int result = -1;
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement("update t_user set isactive = 1,isonline=1 where email=?");
			pstmt.setString(1, email);
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn);
		}
		return result;
	}
	
	/**
	 * 
	 * description: 根据用户名和密码查询用户所有信息
	 * creatTime:2015年10月25日上午9:43:24
	 * @author Link
	 * return type: User
	 *
	 */
	public User queryUserInfo(String email,String pass){
		User user = new User();
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement("select * from t_user where email=? and pass=? and isactive=1 and isdelete=0");
			pstmt.setString(1, email);
			pstmt.setString(2, pass);
			rs = pstmt.executeQuery();
			while(rs.next()){
				user.setId(rs.getInt("id"));
				user.setEmail(rs.getString("email"));
				user.setPass(rs.getString("pass"));
				user.setPhoto(rs.getString("photo"));
				user.setQuestion(rs.getString("question"));
				user.setAnswer(rs.getString("answer"));
				user.setPoint(rs.getInt("point"));
				user.setRegtime(rs.getString("regtime"));
				user.setCurrentTank(rs.getInt("currenttank"));
				user.setLastTank(rs.getInt("lasttank"));
				user.setProfession(rs.getString("profession"));
				user.setSex(rs.getString("sex"));
				user.setBirthdate(rs.getString("birthdate"));
				user.setIp(rs.getString("ip"));
				user.setOnlineTime(rs.getInt("onlinetime"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}
	
	/**
	 * 
	 * description:用户登出修改数据库参数online属性为0
	 * 2015年10月26日 下午4:44:04
	 * @author Link
	 * @param email
	 * @return
	 *
	 */
	public int userLoginOut(String email){
		int result = 0;
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement("update t_user set isonline =0 where email=?");
			pstmt.setString(1, email);
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(pstmt, conn);
		}
		return result;
	}
	
	public static void main(String[] args) {
		//new UserDao().addManager(new User("admin", MDUtil.encodePass("admin"), DateUtil.getDate(new Date())));
	}
	
}










